/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.cea.papyrus.core.editpart.IUMLDiagramElementEditPart;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;

/**
 * 
 */
public class NameDiagramSection extends AbstractPropertySection {


	/**
	 * 
	 */
	protected class MyFocusListener implements org.eclipse.swt.events.FocusListener {
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void focusGained(FocusEvent e){
		}
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void focusLost(FocusEvent e){
			diagram.setName(nameText.getText().trim());
		} 
	};

	/**
	 * 
	 */
	private Diagram diagram;

	/**
	 * 
	 */
	protected MyFocusListener nameListener = new MyFocusListener();

	/**
	 * 
	 */
	private Text nameText;

	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 * 
	 * @see org.eclipse.ui.views.properties.tabbed.ITabbedPropertySection#createControls(org.eclipse.swt.widgets.Composite,
	 * org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory()
		.createFlatFormComposite(parent);
		FormData data;

		nameText = getWidgetFactory().createText(composite, ""); //$NON-NLS-1$
		nameText.setEditable(true);
		nameText.addFocusListener(nameListener);
		data = new FormData();
		data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH);
		data.right = new FormAttachment(100, 0);
		data.top = new FormAttachment(0, 0);
		nameText.setLayoutData(data);

		CLabel nameLabel = getWidgetFactory().createCLabel(composite, "Name:"); //$NON-NLS-1$
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(nameText,
				-ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(nameText, 0, SWT.CENTER);
		nameLabel.setLayoutData(data);

	}

	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	/**
	 * 
	 */
	@Override
	public void refresh() {
		if (!nameText.isDisposed()){
			nameText.removeFocusListener(nameListener);
			if (diagram == null) {
				return;
			}
			nameText.setText(diagram.getName());
			nameText.addFocusListener(nameListener);
		}
	}

	/**
	 * 
	 * 
	 * @param part 
	 * @param selection 
	 */
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Assert.isTrue(selection instanceof IStructuredSelection);
		Object input = ((IStructuredSelection) selection).getFirstElement();
		//Assert.isTrue(input instanceof IUMLDiagramElementEditPart);
		if(input instanceof IUMLDiagramElementEditPart) {
			this.diagram = ((IUMLDiagramElementEditPart) input).getDiagram();	
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(nameText!=null && !nameText.isDisposed())
			nameText.removeFocusListener(nameListener);
	}
}
